﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Web.System.Utils;
using System;
using XCore.Web.Swagger;
using Microsoft.OpenApi.Models;
using XCore.Web.Extensions;

namespace Web.System
{
    public class Startup
    {
        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
            #region 注册Swagger UI
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "总管理员后台接口API",
                    Description = "A simple example ASP.NET Core Web API",
                    TermsOfService = new Uri("http://www.admin.lbanyun.com"),
                });
                c.DocumentFilter<AuthApplyTagDescriptions>();
                c.OperationFilter<SwaggerHeaderParameter>();
                c.CustomSchemaIds(x => x.FullName);//输出类名使用全称
            });
            #endregion
            services.AddDbContext<SystemDbContext>();
            services.AddDbContext<LoggerDbContext>();
            //注入授权过滤器
            services.AddControllers(opt =>
            {
                opt.Filters.Add<SystemAuthorizationFilter>();
            });
            services.AddXCore();
        }
        /// <summary>
        /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="app"></param>
        public void Configure(IApplicationBuilder app)
        {
            //授权中间件，中间件有顺序之分
            app.UseMiddleware<SystemExceptionMiddleware>();
            app.UseMiddleware<SystemAuthorizationMiddleware>();
            app.UseXCore();
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Web.System V1");
            });
        }
    }
}
